package com.zy.explore.generator.infrastructure.core.assembler;

import com.zy.explore.generator.infrastructure.constant.ProjectConstant;
import com.zy.explore.generator.infrastructure.core.enums.FrameworkTypeEnum;
import com.zy.explore.generator.infrastructure.core.frm.BrickInfoFrm;
import com.zy.explore.generator.infrastructure.exception.MessageCode;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * description 砖块信息转换器
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2021年06月05日 16:10
 */
@Slf4j
public class BrickInfoAssembler {

    private BrickInfoAssembler() {
    }

    /**
     * 根据参数获取获取除基路径外的剩余部分路径
     *
     * @param frm 参数对象
     * @return 返回Frm类后缀包路径
     */
    public static String getLastFilePath(BrickInfoFrm frm) {
        String fileName = frm.getFileName();
        try {
            FrameworkTypeEnum frameworkType = frm.getFrameworkType();
            if (frameworkType == FrameworkTypeEnum.DDD || frameworkType == FrameworkTypeEnum.DDD_FEIGN) {
                return getDddClassLastFilePath(frm, fileName) + frm.getFileName();
            }
            // else if (frameworkType == FrameworkTypeEnum.MVC) {
            //     return getMvcClassLastFilePath(fileName) + frm.getFileName();
            // }
            return getCommonClassLastFilePath(frm, fileName) + frm.getFileName();
        } catch (Exception e) {
            log.error("文件名称：{}有误，未匹配到对应的位置", fileName);
            e.printStackTrace();
        }
        return "";

    }

    /**
     * 方法说明：获取公共类文件的最后路径
     *
     * @param frm      请求参数
     * @param fileName 文件名称
     * @return {@link String }
     */
    private static String getCommonClassLastFilePath(BrickInfoFrm frm, String fileName) {
        if (fileName.endsWith("Feign.java")) {
            return "/interfaces/feign";
        }
        if (fileName.endsWith("Facade.java")) {
            return "/interfaces/facade/v1";
        }
        if (fileName.endsWith("Controller.java")) {
            return "/controller/v1";
        }
        if (fileName.endsWith("Assembler.java")) {
            return "/interfaces/assembler";
        }
        if (fileName.endsWith("Validator.java")) {
            return "/interfaces/validator";
        }
        if (fileName.endsWith("Service.java")) {
            return "/application/service";
        }

        if (Objects.equals(fileName, (frm.getTrimPrefix() ? "" : "Jpa") + frm.getTableClassNamePrefix() + ".java")) {
            return "/domain/jpa/entity/";
        }
        if (fileName.endsWith("Repository.java")) {
            return "/domain/jpa/repository/";
        }

        if (Objects.equals((frm.getTrimPrefix() ? "" : "Tk") + frm.getTableClassNamePrefix() + ".java", fileName)) {
            return "/domain/mybatis/entity/";
        }
        if (Objects.equals((frm.getTrimPrefix() ? "" : "Mp") + frm.getTableClassNamePrefix() + ".java", fileName)) {
            return "/domain/mybatis/entity/";
        }
        if (!fileName.endsWith("MongoDao.java") && fileName.endsWith("Dao.java")) {
            return "/domain/mybatis/dao/";
        }
        if (fileName.endsWith("Dao.xml")) {
            return "/domain/mybatis/mapper/";
        }

        if (Objects.equals(fileName, "Mongo" + frm.getTableClassNamePrefix() + ".java")) {
            return "/domain/mongo/entity/";
        }

        if (fileName.startsWith("Mongo") && fileName.endsWith("Dao.java")) {
            return "/domain/mongo/dao/";
        }
        throw MessageCode.FILE_NAME_ERROR.toException();
    }

    /**
     * 方法说明：获取MVC结构的类文件的最后路径
     *
     * @param fileName 文件名称
     * @return {@link String }
     */
    private static String getMvcClassLastFilePath(String fileName) {
        if (fileName.endsWith("Frm.java")) {
            return "/frm/";
        }
        if (fileName.endsWith("VO.java")) {
            return "/vo/";
        }
        if (fileName.endsWith("Assembler.java")) {
            return "/assembler/";
        }
        if (fileName.endsWith("Controller.java")) {
            return "/controller/v1/";
        }
        if (fileName.endsWith("Service.java")) {
            return "/service/";
        }
        if (fileName.endsWith("ServiceImpl.java")) {
            return "/service/impl/";
        }
        throw MessageCode.FILE_NAME_ERROR.toException();
    }

    /**
     * 方法说明：获取DDD结构的类文件的最后路径
     *
     * @param frm      请求参数
     * @param fileName 文件名称
     * @return {@link String }
     */
    private static String getDddClassLastFilePath(BrickInfoFrm frm, String fileName) {
        if (fileName.endsWith("Router.java")) {
            return "/infrastructure/constant/";
        }
        if (fileName.endsWith("Frm.java")) {
            return "/interfaces/frm/";
        }
        if (fileName.endsWith("VO.java")) {
            return "/interfaces/vo/";
        }
        if (fileName.endsWith("Feign.java")) {
            return "/interfaces/feign/";
        }
        if (fileName.endsWith("FeignFallback.java")) {
            return "/interfaces/feign/fallback/";
        }
        if (fileName.endsWith("Assembler.java")) {
            return "/interfaces/assembler/";
        }
        if (fileName.endsWith("Facade.java")) {
            return "/interfaces/facade/v1/";
        }
        if (fileName.endsWith("Validator.java")) {
            return "/interfaces/validator/";
        }
        if (fileName.endsWith("Service.java")) {
            return "/application/service/";
        }
        if (fileName.endsWith("Dao.java")) {
            if (fileName.endsWith("MongoDao.java")) {
                return "/domain/mongo/dao/";
            } else {
                return "/domain/mybatis/dao/";
            }
        }
        if (fileName.startsWith("Mongo")) {
            if (fileName.endsWith("Dao.java")) {
                return "/domain/mongo/dao/";
            }
            return "/domain/mongo/entity/";
        }
        if (frm.getUseJpa()) {
            if (Objects.equals(fileName, (frm.getTrimPrefix() ? "" : "Jpa") + frm.getTableClassNamePrefix() + ".java")) {
                return "/domain/jpa/entity/";
            }
        }

        if (fileName.endsWith("Repository.java")) {
            return "/domain/jpa/repository/";
        }
        if (fileName.endsWith("Dao.xml")) {
            return "/domain/mybatis/mapper/";
        }
        if (frm.getUseTkMybatis() || frm.getUseMybatisPlus()) {
            if (Objects.equals(fileName, (frm.getTrimPrefix() ? "" : "Tk") + frm.getTableClassNamePrefix() + ".java")) {
                return "/domain/mybatis/entity/";
            }
            if (Objects.equals(fileName, (frm.getTrimPrefix() ? "" : "Mp") + frm.getTableClassNamePrefix() + ".java")) {
                return "/domain/mybatis/entity/";
            }
        }
        log.error("文件名称：{}，{}", fileName, MessageCode.FILE_NAME_ERROR.getMessage());
        throw MessageCode.FILE_NAME_ERROR.toException();
    }

    /**
     * 根据参数获取砖块模板路径
     *
     * @param frm 参数对象
     * @return 返回对应砖块模板的路径
     */
    public static String getBrickTemplatePath(BrickInfoFrm frm) {
        FrameworkTypeEnum frameworkType = frm.getFrameworkType();
        String templatePath = "";
        if (frameworkType == FrameworkTypeEnum.DDD || frameworkType == FrameworkTypeEnum.DDD_FEIGN) {
            templatePath = ProjectConstant.SRC_MAIN_RESOURCES + ProjectConstant.DDD_BRICK_TEMPLATE_BASE_PATH;
        }
        // else if (frameworkType == FrameworkTypeEnum.MVC) {
        //     templatePath = ProjectConstant.SRC_MAIN_RESOURCES + ProjectConstant.MVC_BRICK_TEMPLATE_BASE_PATH;
        // }
        String userDir = System.getProperty("user.dir");
        if (!userDir.endsWith(ProjectConstant.APPLICATION_NAME)) {
            templatePath = "/" + ProjectConstant.APPLICATION_NAME + templatePath;
        }
        return templatePath;
    }
}
